package com.ylzx.controller;

import cn.hutool.core.util.NumberUtil;
import com.ylzx.entity.Emp;
import com.ylzx.exception.BusinessException;
import com.ylzx.exception.SystemException;
import com.ylzx.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/emp")
public class EmpController {

    //注入业务层对象
    @Autowired
    private EmpService empService;

    /**
     * 根据编号查询员工
     * @param id 编号
     * @return 员工对象
     */
    @GetMapping("/{id}") //GET请求，必须要带路径参数:/7369
    @ResponseBody //响应JSON
    public Emp findById(@PathVariable("id") String id){
        if(!NumberUtil.isNumber(id)){
            throw new BusinessException("你输入的编号不匹配",Code.SYSTEM_ERR);
        }
        return empService.findById(Integer.parseInt(id));
    }

    /**
     * 查询所有员工
     * @return 员工列表
     */
    @GetMapping("/")
    @ResponseBody
    public Result findAll(){
        Integer code;
        String reason;
        List<Emp> list;
//        try {

            list = empService.findAll();
            code = list == null ? Code.GET_ERR : Code.GET_OK;
            reason = list == null ? "error" : "success";
//        }catch (ArithmeticException ex){
//            throw new SystemException("系统异常",0);
//        }
        return new Result(code, reason, list);
    }

    @GetMapping("/page")
    @ResponseBody
    public Page findPage(Integer pageNum,Integer pageSize){
        List<Emp> list = empService.findPage(pageNum, pageSize);
        Integer code = list==null?Code.GET_ERR:Code.GET_OK;
        String reason = list==null?"数据访问失败！":"数据获取成功!";
        Integer total = empService.findTotal();
        return new Page(code, reason, list, total);
    }


    @PostMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody Emp emp){//前端的对象为JSON类型，后端为Emp类型
        try {
            empService.insert(emp);
            return new Result(Code.ADD_OK,"添加成功",null);
        }catch (Exception e){
            return new Result(Code.ADD_Err,"添加失败",null);
        }
    }

    @PutMapping("/")
    @ResponseBody
    public String update(@RequestBody Emp emp){//前端的对象为JSON类型，后端为Emp类型
        try {
            empService.update(emp);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    @DeleteMapping("/{ids}") // emp/7369,7788,
    @ResponseBody
    public Result delete(@PathVariable("ids") Integer[] ids){//前端的对象为JSON类型，后端为Emp类型
        try {
            empService.delete(ids);
            return new Result(Code.DELETE_OK, "删除成功！", null);
        }catch (Exception e){
            return new Result(Code.DELETE_ERR, "删除失败！", null);
        }
    }

    @RequestMapping("/select01") // emp/7369,7788,
    @ResponseBody
    public ModelAndView select01(String ename){
        //1.调用service方法
        List<Emp> empList = empService.select01(ename);
        //2.实例化一个ModelAndView对象
        ModelAndView mv=new ModelAndView();
        mv.addObject("empList",empList);
        mv.setViewName("emp-list");
        return mv;
    }
}
